package io.github.junxworks.qt.modules.tushare.service.impl;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import io.github.junxworks.qt.modules.data.vo.DStockProfileVo;
import io.github.junxworks.qt.modules.tushare.mapper.BaseInfoMapper;
import io.github.junxworks.qt.modules.tushare.service.BaseInfoService;
import io.github.junxworks.qt.modules.tushare.service.TushareService;
import jakarta.annotation.PostConstruct;

// TODO: Auto-generated Javadoc
/**
 * The Class BaseInfoServiceImpl.
 *
 * @program :springbootdev
 * @description: 
 * @author:Junxworks 
 * @create:2019-08-16 19:51
 */
@Service
public class BaseInfoServiceImpl extends TushareService implements BaseInfoService {

	/** The available profiles cache. */
	private Map<String, DStockProfileVo> availableProfilesCache = Maps.newHashMap();

	private List<Integer> financePeriods;

	/** The base info mapper. */
	@Autowired
	private BaseInfoMapper baseInfoMapper;

	/**
	 * Inits the.
	 */
	@PostConstruct
	public void init() {
		refreshStockProfilesCache();
		initFinancePeriods();
	}

	private void initFinancePeriods() {
		financePeriods = baseInfoMapper.queryFinancePeriods();
	}

	/**
	 * Gets the profile by stock code.
	 *
	 * @param stockCode the stock code
	 * @return the profile by stock code
	 */
	@Override
	public DStockProfileVo getProfileByTsCode(String tsCode) {
		return baseInfoMapper.queryStockProfileByCode(tsCode);
	}

	/**
	 * Gets the profile from cache.
	 *
	 * @param stockCode the stock code
	 * @return the profile from cache
	 */
	@Override
	public DStockProfileVo getProfileFromCache(String tsCode) {
		return profilesCache.get(tsCode);
	}

	/**
	 * Refresh stock profiles cache.
	 */
	@Override
	public void refreshStockProfilesCache() {
		List<DStockProfileVo> os = baseInfoMapper.queryAllStockProfiles();
		if (!os.isEmpty()) {
			profilesCache.clear();
			profilesCache.putAll(os.stream().collect(Collectors.toMap(DStockProfileVo::getTsCode, v -> v)));
		}
		os = baseInfoMapper.queryAllAvailableStockProfiles();
		if (!os.isEmpty()) {
			availableProfilesCache = os.stream().collect(Collectors.toMap(DStockProfileVo::getTsCode, v -> v));
		}
	}

	/**
	 * Gets the all profiles.
	 *
	 * @return the all profiles
	 */
	@Override
	public List<DStockProfileVo> getAllProfiles() {
		if (!profilesCache.values().isEmpty()) {
			return Lists.newArrayList(profilesCache.values());
		}
		return baseInfoMapper.queryAllStockProfiles();
	}

	/**
	 * Gets the all availabe profiles.
	 *
	 * @return the all availabe profiles
	 */
	@Override
	public List<DStockProfileVo> getAllAvailabeProfiles() {
		if (!availableProfilesCache.values().isEmpty()) {
			return Lists.newArrayList(availableProfilesCache.values());
		}
		return baseInfoMapper.queryAllAvailableStockProfiles();
	}

	@Override
	public DStockProfileVo getProfileBySymbol(String symbol) {
		return baseInfoMapper.queryStockProfileBySymbol(symbol);
	}

	@Override
	public List<Integer> getFinancePeriods() {
		return financePeriods;
	}
}
